package cn.edu.zjut.mina.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.edu.zjut.mina.bean.RoomInfo;
import cn.edu.zjut.mina.bean.StandardFormat;
import cn.edu.zjut.mina.bean.UserSession;
import cn.edu.zjut.mina.interf.IRoomInfo;
import cn.edu.zjut.mina.po.EveryGameResult;
import cn.edu.zjut.mina.po.PlayerBattleInfo;
import cn.edu.zjut.mina.po.UserInfo;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.service.IEveryGameBeginService;
import cn.edu.zjut.mina.service.IGameOverService;
import cn.edu.zjut.mina.service.INotificationService;
import cn.edu.zjut.mina.util.GsonUtils;
import cn.edu.zjut.mina.util.ResultInfoConstUtils;
import cn.edu.zjut.mina.util.UserSessionMap;

@Service("notificationService")
public class NotificationServiceImpl implements INotificationService{
	@Qualifier("everyGameBeginService")
	@Autowired
	private IEveryGameBeginService everyGameBeginService;
	/**
	 * 用于存放用户信息的集合
	 */
	private final UserSessionMap userSessionMap = UserSessionMap.getInstance();
	
	@Qualifier("gameOverService")
	@Autowired
	private IGameOverService gameOverService;
	
	/**
	 * 通知加入房间
	 * @param roomId 房间号
	 * @param userId4Join 加入者的userId
	 */
	public void notificationJoinRoom(String roomId, String userId4Join) {//通知加入房间
		// TODO Auto-generated method stub

		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//获取主动加入房间者收到的消息
		JMessageProtocalRes resInitiative = getAllUserInfo4JoinRoomInitiative(roomId, userId4Join);
		resInitiative.setResultCode(ProtocalRegulation.JOINROOMINITIATIVE);
		//获取已经在房间者收到的其他玩家加入房间的消息
		JMessageProtocalRes resPassivity = getAllUserInfo4JoinRoomPassivity(roomId, userId4Join);
		resPassivity.setResultCode(ProtocalRegulation.JOINROOMPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Join)) {
				session.write(resInitiative);
			} else {
				session.write(resPassivity);
			}
			
		}
	}

	/**
	 * 通知房间内 玩家的准备信息
	 * @param roomId
	 * @param userId4Prepare 开始准备的玩家Id
	 */
	public void notificationPrepare(String roomId, String userId4Prepare) {//通知准备
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//获取主动准备者收到的消息
		JMessageProtocalRes resInitiative = getAllUserInfo4PrepareInitiative(roomId, userId4Prepare);
		resInitiative.setResultCode(ProtocalRegulation.PREPAREINITIATIVE);
		//获取已经在房间者收到其他玩家准备的消息
		JMessageProtocalRes resPassivity = getAllUserInfo4PreparePassivity(roomId, userId4Prepare);
		resPassivity.setResultCode(ProtocalRegulation.PREPAREPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Prepare)) 
				session.write(resInitiative);
			else
				session.write(resPassivity);
		}
	}
	
	/**
	 * 通知房间内 玩家的接受消息
	 * @param roomId
	 * @param userId4Prepare 开始准备的玩家Id
	 */
	public void notificationSendMessage(String roomId, String userId4SendMessage) {//通知消息
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		JMessageProtocalRes resInitiative = getAllUserInfo4SendMessageInitiative(roomId, userId4SendMessage);
		resInitiative.setResultCode(ProtocalRegulation.SENDMESSAGEINITIATIVE);
		
		JMessageProtocalRes resPassivity = getAllUserInfo4SendMessagePassivity(roomId, userId4SendMessage);
		resPassivity.setResultCode(ProtocalRegulation.SENDMESSAGEPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4SendMessage)) 
				session.write(resInitiative);
			else
				session.write(resPassivity);
		}
	}
	
	public void notificationDeal(String roomId) {//通知发牌
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		JMessageProtocalRes res = getAllUserInfo4Deal(roomId);
		res.setResultCode(ProtocalRegulation.DEAL);
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			session.write(res);
		}
	}
	
	public void notificationShowCards(String roomId, String userId4ShowCards) {//通知亮牌
		// TODO Auto-generated method stub
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		
		//自己收到亮牌消息
		JMessageProtocalRes resInitiative = getAllUserInfo4ShowCardsInitiative(roomId, userId4ShowCards);
		resInitiative.setResultCode(ProtocalRegulation.SHOWCARDSINITIATIVE);
		//其他人收到亮牌消息
		JMessageProtocalRes resPassivity = getAllUserInfo4ShowCardsPassivity(roomId, userId4ShowCards);
		resPassivity.setResultCode(ProtocalRegulation.SHOWCARDSPASSIVITY);
		
		
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4ShowCards)) {
				session.write(resInitiative);
			} else {
				session.write(resPassivity);
			}
		}
		
		System.out.println(roomInfo.getActualParticipation() + "   " + roomInfo.getShowCardsNum()
				+ "   " + roomInfo.getActual_number_of_games() + " " + roomInfo.getNumber_of_games()
				+ " ===========================");
		
		
		//所有人都亮牌了表明这局游戏结束了
		if (roomInfo.getShowCardsNum() == roomInfo.getParticipation()) {
			StandardFormat inJson=new StandardFormat();
			inJson.roomId=roomId;
			System.out.println("正常游戏结算........");
			//结算游戏
			everyGameBeginService.deal(inJson, null);
			System.out.println("正常游戏结算成功........");
			
			for (int index = 0; index < roomInfo.getActualParticipation(); index++) {
				roomInfo.getUserSessions().get(index).user.setShowCards(0);
				roomInfo.getUserSessions().get(index).user.setPrepare(0);
				roomInfo.getUserSessions().get(index).user.setCards(null);
			}
			// 结束这场游戏
			if (roomInfo.getActual_number_of_games() == roomInfo.getNumber_of_games() && roomInfo.getShowCardsNum() == roomInfo.getActualParticipation()) {
				gameOverService.clearRoomInfo(roomInfo);
			}
			roomInfo.setShowCardsNum(0);
			roomInfo.setActualPrepare(0);
		}
		
		
	}

	
	/**
	 *重新发送信息
	 * @param roomId 房间号
	 * @param userId4Recover 恢复 者的userId
	 */
	public void notificationRecoverInGame(String roomId, String userId4Recover) {//通知恢复数据
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		
		if (roomInfo.getShowCardsNum() == roomInfo.getParticipation()) {
			StandardFormat inJson=new StandardFormat();
			inJson.roomId=roomId;
			
			System.out.println("重连游戏结算........");
			//结算游戏
			everyGameBeginService.deal(inJson, null);
			System.out.println("重连游戏结算成功........");
			
			
			for (int index = 0; index < roomInfo.getActualParticipation(); index++) {
				roomInfo.getUserSessions().get(index).user.setShowCards(0);
				roomInfo.getUserSessions().get(index).user.setPrepare(0);
				roomInfo.getUserSessions().get(index).user.setCards(null);
			}
			// 结束这场游戏
			if (roomInfo.getActual_number_of_games() == roomInfo.getNumber_of_games() && roomInfo.getShowCardsNum() == roomInfo.getActualParticipation()) {
				gameOverService.clearRoomInfo(roomInfo);
			}
			roomInfo.setShowCardsNum(0);
			roomInfo.setActualPrepare(0);
		}
		
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//获取主动加入房间者收到的信息
		JMessageProtocalRes resInitiative = getAllUserInfo4RecoverInitiative(roomId, userId4Recover);
		resInitiative.setResultCode(ProtocalRegulation.RECOVERINITIATIVE);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Recover) && session != null) {
				session.write(resInitiative);
				break;
			} 
		}
		
	}
	

	
	
	
	/**
	 * 通知加入房间
	 * @param roomId 房间号
	 * @param userId4Recover 恢复 者的userId
	 */
	public void notificationRecover(String roomId, String userId4Recover) {//通知恢复数据
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//获取主动加入房间者收到的信息
		JMessageProtocalRes resInitiative = getAllUserInfo4RecoverInitiative(roomId, userId4Recover);
		resInitiative.setResultCode(ProtocalRegulation.RECOVERINITIATIVE);
		//获取已经在房间者收到的信息
		JMessageProtocalRes resPassivity = getAllUserInfo4RecoverPassivity(roomId, userId4Recover);
		resPassivity.setResultCode(ProtocalRegulation.RECOVERPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Recover) && session != null) {
				session.write(resInitiative);
			} else {
				if (session != null) {
					session.write(resPassivity);
				}
			}
			
		}
		
	}
	
	
	/**
	 * 通知退出房间
	 * @param roomId 房间号
	 * @param userId4Quit 加入者的userId
	 */
	public void notificationQuitRoom(String roomId, String userId4Quit) {//通知退出房间
		// TODO Auto-generated method stub

		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//获取主动退出房间者收到的信息
		JMessageProtocalRes resInitiative = getAllUserInfo4QuitInitiative(roomId, userId4Quit);
		resInitiative.setResultCode(ProtocalRegulation.QUITROOMINITIATIVE);
		//获取已经在房间者收到的信息
		JMessageProtocalRes resPassivity = getAllUserInfo4QuitPassivity(roomId, userId4Quit);
		resPassivity.setResultCode(ProtocalRegulation.QUITROOMPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Quit) && session != null) {
				session.write(resInitiative);
			} else {
				if (session != null) 
					session.write(resPassivity);
			}
			
		}
		
	}
	
	/**
	 * 通知解散房间
	 * @param roomId 房间号
	 * @param dissolveOrRemain  解散或者保留房间   1   解散     0     保留   
	 * @param userid 发起者id
	 * @param type 0  申请解散的请求     1  提交解散的请求
	 */
	public void notificationDissolveOrRemainRoom(String roomId, String userid, int dissolveOrRemain, int type) {//通知解散房间
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "解散房间：成功";
		outJson.result = true;
		outJson.dissolveOrRemain = dissolveOrRemain;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userid) && session != null) {
				if (type == QuitRoomServiceImpl.QUITTYPE) {
					res.setResultCode(ProtocalRegulation.DISSOLVEORREMAINROOM);
				} else if (type == QuitRoomServiceImpl.APPLYQUITTYPE) {
					res.setResultCode(ProtocalRegulation.APPLYQUITROOMDISSOLVEORREMAINROOM);
				} else  if (type == QuitRoomServiceImpl.ROOMOWNERDISSOLVEROOM) {
					res.setResultCode(ProtocalRegulation.ROOMOWNERDISSOLVEROOMINITIATIVE);
				}
				session.write(res);
			} else {
				if (session != null) {
					if (type == QuitRoomServiceImpl.QUITTYPE) {
						res.setResultCode(ProtocalRegulation.DISSOLVEORREMAINROOM);
					} else if (type == QuitRoomServiceImpl.APPLYQUITTYPE) {
						res.setResultCode(ProtocalRegulation.APPLYQUITROOMDISSOLVEORREMAINROOM);
					} else  if (type == QuitRoomServiceImpl.ROOMOWNERDISSOLVEROOM) {
						res.setResultCode(ProtocalRegulation.ROOMOWNERDISSOLVEROOMPASSIVITY);
					}
					session.write(res);
				}
			}
			
		}
		
		for (int i = 0; i < userSessionList.size(); i++) {
			userSessionList.get(i).user.setQuit(0);//退房属性初始化
		}
		if (dissolveOrRemain == 1 && type != QuitRoomServiceImpl.ROOMOWNERDISSOLVEROOM) {//统计游戏结果和清除房间数据
			gameOverService.statisticalResult(roomInfo);
			gameOverService.clearRoomInfo(roomInfo);
		} else if (dissolveOrRemain == 1 && type == QuitRoomServiceImpl.ROOMOWNERDISSOLVEROOM) {
			//初始化
			EveryGameResult eveyGameResult = new EveryGameResult();
			//设置总的游戏结果
			eveyGameResult.setTotalGameResult(roomInfo.getTotalGameResult());
			//玩家每局的对战信息
			List<PlayerBattleInfo> playerBattleInfos = new ArrayList<PlayerBattleInfo>();
			for (int i = 0; i < roomInfo.getActualParticipation(); i++) {
				PlayerBattleInfo playerBattleInfo = new PlayerBattleInfo();
				playerBattleInfo.setEveryGameResult(eveyGameResult);
				UserInfo user = roomInfo.getUserSessions().get(i).user;
				playerBattleInfo.setUserId(user.getUserId());
				playerBattleInfos.add(playerBattleInfo);
			}
			eveyGameResult.setPlayerBattleInfos(playerBattleInfos);
			if (roomInfo.getTotalGameResult().getEveryGameResult() == null) {
				roomInfo.getTotalGameResult().setEveryGameResult(new ArrayList<EveryGameResult>());
			}
			
			roomInfo.getTotalGameResult().getEveryGameResult().add(eveyGameResult);
			roomInfo.getTotalGameResult().setConstraintQuitRoom(1);//强制退出标记
			gameOverService.statisticalResult(roomInfo);
			gameOverService.clearRoomInfo(roomInfo);
			
		}
		
		
	}
	
	/**
	 * 通知房间内 返回大厅信息
	 * @param roomId
	 * @param userId4Player 玩家Id
	 */
	public void notificationBackToHaLL(String roomId, String userId4Player) {
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//自己
		JMessageProtocalRes resInitiative = getAllUserInfo4BackToHaLLInitiative(roomId, userId4Player);
		resInitiative.setResultCode(ProtocalRegulation.BACKTOHALLINITIATIVE);
		//他人
		JMessageProtocalRes resPassivity = getAllUserInfo4BackToHaLLPassivity(roomId, userId4Player);
		resPassivity.setResultCode(ProtocalRegulation.BACKTOHALLPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Player)) 
				session.write(resInitiative);
			else
				session.write(resPassivity);
		}
	}
	
	/**
	 * 通知房间内 返回游戏房间信息
	 * @param roomId
	 * @param userId4Banker 庄家Id(只有庄家有返回房间的功能，因为目前庄家是开房者)
	 */
	public void notificationBackToRoom(String roomId, String userId4Banker) {
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//自己
		JMessageProtocalRes resInitiative = getAllUserInfo4JoinRoomInitiative(roomId, userId4Banker);
		resInitiative.setResultCode(ProtocalRegulation.BACKTOROOMINITIATIVE);
		//他人
		JMessageProtocalRes resPassivity = getAllUserInfo4JoinRoomPassivity(roomId, userId4Banker);
		resPassivity.setResultCode(ProtocalRegulation.BACKTOROOMPASSIVITY);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (userSessionList.get(i).user.getUserId().equals(userId4Banker)) 
				session.write(resInitiative);
			else
				session.write(resPassivity);
		}
	}
	
	
	/**
	 * 通知房间内 的玩家掉线信息
	 * @param roomId
	 * @param userId4Banker 庄家Id(只有庄家有返回房间的功能，因为目前庄家是开房者)
	 */
	public void notificationLoseConnection(String roomId, String userId4Lose) {
		// TODO Auto-generated method stub
		
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		
		JMessageProtocalRes resPassivity = getAllUserInfo4LoseConnectionPassivity(roomId, userId4Lose);
		resPassivity.setResultCode(ProtocalRegulation.LOSECONNECTION);
		
		for (int i = 0; i < userSessionList.size(); i++) {
			IoSession session = userSessionList.get(i).session;
			if (!userSessionList.get(i).user.getUserId().equals(userId4Lose)) 
				session.write(resPassivity);
		}
	}
	
	/**
	 * 获取房间中所有用户的信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo (String roomId) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			userInfoList.add(userSessionList.get(i).user);
		}
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "获取房间中所有用户的信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 获取牌信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4Deal (String roomId) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			UserInfo user = new UserInfo();
			user.setUserId(userSessionList.get(i).user.getUserId());
			user.setPosition((userSessionList.get(i).user.getPosition()));
			
			user.setCards(userSessionList.get(i).user.getCards());
			userInfoList.add(user);
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "获取牌信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 亮牌玩家获取信息
	 * @param roomId
	 * @param userId4ShowCards
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4ShowCardsInitiative (String roomId, String userId4ShowCards) {
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = ResultInfoConstUtils.SHOWCARD_MYSUCCESSINFO;
		outJson.result = true;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 房间里的玩家收到其他人亮牌的信息
	 * @param roomId
	 * @param userId4ShowCards
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4ShowCardsPassivity (String roomId, String userId4ShowCards) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4ShowCards)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setShowCards(userSessionList.get(i).user.getShowCards());
				userInfoList.add(user);
			}
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = ResultInfoConstUtils.SHOWCARD_OTHERSUCCESSINFO;
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 发送消息的玩家获取的信息
	 * @param roomId
	 * @param userId4SendMessage
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4SendMessageInitiative (String roomId, String userId4SendMessage) {
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "发送消息的玩家获取的信息：成功";
		outJson.result = true;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 房间里的玩家收到其他人发送的消息
	 * @param roomId
	 * @param userId4SendMessage
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4SendMessagePassivity (String roomId, String userId4SendMessage) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4SendMessage)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setSendMessage(userSessionList.get(i).user.getSendMessage());
				user.setMessage(userSessionList.get(i).user.getMessage());
				userInfoList.add(user);
			}
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "房间里的玩家收到其他人发送的消息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 加入房间者获取的信息(获取房间内所有人的信息)
	 * @param roomId
	 * @param userId4Join
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4JoinRoomInitiative (String roomId, String userId4Join) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		//存放返回给客户端的用户信息集合
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合,包括自己
			UserInfo user = new UserInfo();
			user.setUserId(userSessionList.get(i).user.getUserId());
			user.setNickName(userSessionList.get(i).user.getNickName());
			user.setPosition((userSessionList.get(i).user.getPosition()));
			user.setHeadimgurl(userSessionList.get(i).user.getHeadimgurl());
			user.setIp(userSessionList.get(i).user.getIp());
			user.setSex(userSessionList.get(i).user.getSex());
			user.setSex(userSessionList.get(i).user.getSex());
			user.initPlayerInfo();
			//设置准备字段
			if (userSessionList.get(i).user.getPrepare() == null || userSessionList.get(i).user.getPrepare() == 0) {
				user.setPrepare(0);
			} else 
				user.setPrepare(1);
			//设置断线字段
			if (userSessionList.get(i).user.getLostConnection() != null && userSessionList.get(i).user.getLostConnection() == 1) {
				user.setLostConnection(1);
			} else 
				user.setLostConnection(0);
			
			userInfoList.add(user);
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		//返回文字信息
		outJson.resultInfo = ResultInfoConstUtils.FINDROOM_MYSUCCESSINFO;
		outJson.result = true;
		outJson.roomId = roomId;
		//以下为房间信息

		outJson.gameType=roomInfo.getGameType();
		outJson.participation = roomInfo.getParticipation();
		outJson.number_of_games = roomInfo.getNumber_of_games();
		outJson.ratio = roomInfo.getRatio();
		outJson.lzxs = roomInfo.getLzxs();
		outJson.criticalOfTwoTimes = roomInfo.getCriticalOfTwoTimes();
		outJson.deductRoomCard = roomInfo.getDeductRoomCard();
		outJson.AllUser = userInfoList;
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 已经在房间里的人收到加入者的信息
	 * @param roomId
	 * @param userId4Join
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4JoinRoomPassivity (String roomId, String userId4Join) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			//其他用户只需要获取刚加入房间的玩家信息
			if (userSessionList.get(i).user.getUserId().equals(userId4Join)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setNickName(userSessionList.get(i).user.getNickName());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setHeadimgurl(userSessionList.get(i).user.getHeadimgurl());
				user.setIp(userSessionList.get(i).user.getIp());
				user.setSex(userSessionList.get(i).user.getSex());
				userInfoList.add(user);
			}
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = ResultInfoConstUtils.FINDROOM_OTHERSUCCESSINFO;
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 发出准备的玩家收到的信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4PrepareInitiative (String roomId, String userId4Prepare) {
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "发出准备的玩家收到的信息：成功";
		outJson.result = true;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 收到别人的准备信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4PreparePassivity (String roomId, String userId4Prepare) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4Prepare)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setPrepare((userSessionList.get(i).user.getPrepare()));
				userInfoList.add(user);
			}
			
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "收到别人的准备信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 恢复数据者获取的信息(获取房间内所有人的信息)
	 * @param roomId
	 * @param userId4Recover
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4RecoverInitiative (String roomId, String userId4Recover) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			UserInfo user = new UserInfo();
			user.setUserId(userSessionList.get(i).user.getUserId());
			user.setNickName(userSessionList.get(i).user.getNickName());
			user.setPosition((userSessionList.get(i).user.getPosition()));
			user.setHeadimgurl(userSessionList.get(i).user.getHeadimgurl());
			user.setSex(userSessionList.get(i).user.getSex());
			user.setIp(userSessionList.get(i).user.getIp());
			if (userSessionList.get(i).user.getPrepare() == null || userSessionList.get(i).user.getPrepare() == 0) {
				user.setPrepare(0);
			} else 
				user.setPrepare(1);
			if (userSessionList.get(i).user.getShowCards() == null || userSessionList.get(i).user.getShowCards() == 0) {
				user.setShowCards(0);
			} else 
				user.setShowCards(1);
			if (userSessionList.get(i).user.getLostConnection() == null || userSessionList.get(i).user.getLostConnection() == 0) {
				user.setLostConnection(0);
			} else 
				user.setLostConnection(1);
			if (userSessionList.get(i).user.getCards() == null) {
				user.setCards(null);
			} else 
				user.setCards(userSessionList.get(i).user.getCards());
			
			
			user.setmCountOfNiuNiu(userSessionList.get(i).user.getmCountOfNiuNiu());
			user.setmCountOfWuHua(userSessionList.get(i).user.getmCountOfWuHua());
			user.setmCountOfYouNiu(userSessionList.get(i).user.getmCountOfYouNiu());
			user.setmLastRoundPoNiu(userSessionList.get(i).user.ismLastRoundPoNiu());
			user.setCredits(userSessionList.get(i).user.getCredits());
			
			
			userInfoList.add(user);
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = ResultInfoConstUtils.RECOVER_SUCCESSINFO;
		outJson.result = true;
		outJson.roomId = roomId;
		outJson.participation = roomInfo.getParticipation();
		outJson.number_of_games = roomInfo.getNumber_of_games();
		outJson.ratio = roomInfo.getRatio();
		outJson.lzxs = roomInfo.getLzxs();
		outJson.criticalOfTwoTimes = roomInfo.getCriticalOfTwoTimes();
		outJson.deductRoomCard = roomInfo.getDeductRoomCard();
		outJson.actualNumberOfGames = roomInfo.getActual_number_of_games();
		outJson.bankerNumber=roomInfo.getBankerNumber();
		
		outJson.AllUser = userInfoList;
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 已经在房间里的人收到恢复数据者的信息
	 * @param roomId
	 * @param userId4Recover
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4RecoverPassivity (String roomId, String userId4Recover) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {
			//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4Recover)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setNickName(userSessionList.get(i).user.getNickName());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setHeadimgurl(userSessionList.get(i).user.getHeadimgurl());
				user.setIp(userSessionList.get(i).user.getIp());
				user.setSex(userSessionList.get(i).user.getSex());
				if (userSessionList.get(i).user.getPrepare() == null || userSessionList.get(i).user.getPrepare() == 0) {
					user.setPrepare(0);
				} else 
					user.setPrepare(1);
				if (userSessionList.get(i).user.getShowCards() == null || userSessionList.get(i).user.getShowCards() == 0) {
					user.setShowCards(0);
				} else 
					user.setShowCards(1);
				if (userSessionList.get(i).user.getLostConnection() == null || userSessionList.get(i).user.getLostConnection() == 0) {
					user.setLostConnection(0);
				} else 
					user.setLostConnection(1);
				if (userSessionList.get(i).user.getCards() == null) {
					user.setCards(null);
				} else 
					user.setCards(userSessionList.get(i).user.getCards());
				userInfoList.add(user);
			}
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "已经在房间里的人收到恢复数据者的信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 发出退出消息的玩家收到的信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4QuitInitiative (String roomId, String userId4Quit) {
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "发出退出消息的玩家收到的信息：成功";
		outJson.result = true;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 收到别人的退出信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4QuitPassivity (String roomId, String userId4Quit) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4Quit)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setQuit(userSessionList.get(i).user.getQuit());
				userInfoList.add(user);
			}
			
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "收到别人的退出信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 返回大厅收到的信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4BackToHaLLInitiative (String roomId, String userId4Player) {
		
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "返回大厅收到的信息：成功";
		outJson.result = true;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 其他人收到返回大厅的信息
	 * @param roomId
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4BackToHaLLPassivity (String roomId, String userId4Player) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4Player)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				user.setPrepare(0);
				user.setBacktohall(1);
				userInfoList.add(user);
			}
			
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "其他人收到返回大厅的信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
	/**
	 * 已经在房间里的人收到加入者的信息
	 * @param roomId
	 * @param userId4Join
	 * @return
	 */
	JMessageProtocalRes getAllUserInfo4LoseConnectionPassivity (String roomId, String userId4Join) {
		IRoomInfo roomInfo = userSessionMap.get(roomId);
		List<UserSession> userSessionList = roomInfo.getUserSessions();
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		for (int i = 0; i < userSessionList.size(); i++) {//获取当前用户集合
			if (userSessionList.get(i).user.getUserId().equals(userId4Join)) {
				UserInfo user = new UserInfo();
				user.setUserId(userSessionList.get(i).user.getUserId());
				user.setPosition((userSessionList.get(i).user.getPosition()));
				if (userSessionList.get(i).user.getLostConnection() != null && userSessionList.get(i).user.getLostConnection() == 1) {
					user.setLostConnection(1);
				} else 
					user.setLostConnection(0);
				if (userSessionList.get(i).user.getPrepare() != null && userSessionList.get(i).user.getPrepare() == 1) {
					user.setPrepare(1);
				} else 
					user.setPrepare(0);
				if (userSessionList.get(i).user.getShowCards() != null && userSessionList.get(i).user.getShowCards() == 1) {
					user.setShowCards(1);
				} else 
					user.setShowCards(0);
				userInfoList.add(user);
			}
		}
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		outJson.resultInfo = "已经在房间里的人收到加入者的信息：成功";
		outJson.result = true;
		outJson.AllUser = userInfoList;
		
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}

}
